' COI Trustee Disclosure Statement - Requirement Script - GVR
Option Strict Off
imports System
imports Humans=PHSRAG.Insight21.Humans
imports COI = PHSRAG.Insight21.COI
imports PHSRAG.Insight21.BusinessObjects
imports PHSRAG.Insight21.FormsManagement
imports System.Collections
imports Utility=PHSRAG.Utility
imports PHSRAG.Insight21.Policy

Module Script
    Sub Main()
							
		Dim answers As System.Collections.ArrayList
		Dim answer As Answer
		Dim questionnaireAnswer as QuestionnaireAnswer
	    Dim _cachePolicy as ICachePolicy
	    '_cachePolicy = (Policy.ICachePolicy) Session[Policy.Names.CachePolicyKey]
		currentCOI.LoadQuestionnaireAnswers()
		
		'Verifying if equity interests have to be disclosed needs to be filled out
		answers = questAnswer.GetAnswers("YourEquityInterests")
		Dim answersYoursAssociations As System.Collections.ArrayList
        answersYoursAssociations = questAnswer.GetAnswers("YourAssociations")
        Dim answerYourAssociations As Answer
        
		If ( answers.Count > 0 OR answersYoursAssociations.Count > 0 ) Then
			
			'answer = answers.Count > 0 ? (Answer)answers[0] : new Answer(_cachePolicy)
			'answerYourAssociations = answersYoursAssociations.Count > 0 ? (Answer)answersYoursAssociations[0] : new Answer(_cachePolicy)
			IF (answers.count > 0) THEN
			    answer = answers(0)
			ELSE
			    answer = new Answer(_cachePolicy)
			END IF
            
            IF (answersYoursAssociations.Count > 0) THEN
                answerYourAssociations = answersYoursAssociations(0)
            ELSE
                answerYourAssociations = new Answer(_cachePolicy)
            END IF

			If(answer.Value = "Y" OR answerYourAssociations.Value ="Y") Then
				currentCOI.HasEquityInterests = "Y"
				'Get the active questionnaireversion no.
				dim equityInterestQV as QuestionnaireVersion = _
						Questionnaire.GetActiveVersion(cachePolicy, "Trustee Association", _
							PHSRAG.Insight21.FormsManagement.Constants.QuestionnaireType.COI)
				
				answers = questAnswer.GetAnswers("ListEquityInterests")
				answersYoursAssociations = questAnswer.GetAnswers("ListAssociations")
				
				'Club the two array lists
				Dim answer_yu as Answer
                For Each answer_yu IN answersYoursAssociations
                    Dim found as boolean = false
                    Dim answer1 as Answer
                    For Each answer1 IN answers
                        IF answer1.Value = answer_yu.Value Then
                            found = true
                            exit for
                        End If 
                    Next
                    If NOT found Then
                        answers.Add(answer_yu)
                    End If
'                    If NOT answers.Contains(AYU) Then
'                        answers.Add(AYU);
'                    End If
                Next
                
				'Delete the EI which are no longer applicable
				For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
					If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_EI" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Trustee Association") Then
						dim found as boolean = false
						For Each answer in answers
							dim ei as string() = answer.Value.Split("|")
							if (questionnaireAnswer.Name = ei(1)) then
								found = true
								exit for
							end if
						Next
						if not found then
							currentCOI.RemoveQuestionnaireAnswer(questionnaireAnswer.Id)
						end if
					End If
				Next	

				For Each answer in answers
					dim ei as string() = answer.Value.Split("|")
					'add OA questionnaire answer if it is not added yet
					dim found as boolean = false
					For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
						If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_EI" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Trustee Association") Then
							if (questionnaireAnswer.Name = ei(1)) then
								found = true
								exit for
							end if
						End If
					Next
					
					if not found then
						dim qa as QuestionnaireAnswer = new QuestionnaireAnswer(cachePolicy)
						qa.QuestionnaireVersionId = equityInterestQV.Id
						qa.Name = ei(1)
						qa.ExternalId = ei(0)
						
						dim outsideEntityAnswer as new Answer(cachePolicy)
						outsideEntityAnswer.QuestionnaireAnswerId = qa.Id

						outsideEntityAnswer.Name = "OutsideEntityName"
						outsideEntityAnswer.Value = ei(1)
						
						qa.Answers.Add(outsideEntityAnswer)
						currentCOI.QuestionnaireAnswers.Add(qa)
						currentCOI.Save(connectionString) 'takes care of the mapping record
					end if								
				Next
			Else
				currentCOI.HasEquityInterests = "N"
				For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
				If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_EI" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Trustee Association") Then
					currentCOI.RemoveQuestionnaireAnswer(questionnaireAnswer.Id)
				End If
			Next
			End If							
		End If
		
		'Verifying if family associations have to be disclosed needs to be filled out
		answers = questAnswer.GetAnswers("YourFamily")
		answersYoursAssociations = questAnswer.GetAnswers("YourFamilyEquity")'Newly added question
		If ( answers.Count > 0 OR answersYoursAssociations.Count > 0 ) Then
		    
            IF (answers.count > 0) THEN
			    answer = answers(0)
			ELSE
			    answer = new Answer(_cachePolicy)
			END IF
            
            IF (answersYoursAssociations.Count > 0) THEN
                answerYourAssociations = answersYoursAssociations(0)
            ELSE
                answerYourAssociations = new Answer(_cachePolicy)
            END IF

			'answer = answers(0)
			If(answer.Value = "Y" OR answerYourAssociations.Value ="Y") Then
				currentCOI.HasFamilyAssociations = "Y"
				'Get the active questionnaireversion no.
				dim familyAssociationQV as QuestionnaireVersion = _
						Questionnaire.GetActiveVersion(cachePolicy, "Trustee's Family Associations and Equity Interests", _
							PHSRAG.Insight21.FormsManagement.Constants.QuestionnaireType.COI)
				
				answers = questAnswer.GetAnswers("ListFamilyAssociations")
                answersYoursAssociations = questAnswer.GetAnswers("ListFamilyEquity")'Newly added question
                
                'Club the two array lists
				Dim answer_yu as Answer
                For Each answer_yu IN answersYoursAssociations
                    Dim found as boolean = false
                    Dim answer1 as Answer
                    For Each answer1 IN answers
                        IF answer1.Value = answer_yu.Value Then
                            found = true
                            exit for
                        End If 
                    Next
                    If NOT found Then
                        answers.Add(answer_yu)
                    End If
'                    If NOT answers.Contains(AYU) Then
'                        answers.Add(AYU);
'                    End If
                Next
                
				'Delete the OA which are no longer applicable
				For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
					If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_FA" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Trustee's Family Associations and Equity Interests") Then
						dim found as boolean = false
						For Each answer in answers
							dim fa as string() = answer.Value.Split("|")
							if (questionnaireAnswer.Name = fa(1)) then
								found = true
								exit for
							end if
						Next
						if not found then
							currentCOI.RemoveQuestionnaireAnswer(questionnaireAnswer.Id)
						end if
					End If
				Next	

				For Each answer in answers
					dim fa as string() = answer.Value.Split("|")
					'add OA questionnaire answer if it is not added yet
					dim found as boolean = false
					For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
						If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_FA" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Trustee's Family Associations and Equity Interests") Then
							if (questionnaireAnswer.Name = fa(1)) then
								found = true
								exit for
							end if
						End If
					Next
					
					if not found then
						dim qa as QuestionnaireAnswer = new QuestionnaireAnswer(cachePolicy)
						qa.QuestionnaireVersionId = familyAssociationQV.Id
						qa.Name = fa(1)
						qa.ExternalId = fa(0)
						
						dim outsideEntityAnswer as new Answer(cachePolicy)
						outsideEntityAnswer.QuestionnaireAnswerId = qa.Id

						outsideEntityAnswer.Name = "OutsideEntityName"
						outsideEntityAnswer.Value = fa(1)
						
						
						qa.Answers.Add(outsideEntityAnswer)
						currentCOI.QuestionnaireAnswers.Add(qa)
						currentCOI.Save(connectionString) 'takes care of the mapping record
					end if								
				Next
				
			Else
				currentCOI.HasFamilyAssociations = "N"
				For Each questionnaireAnswer in currentCOI.QuestionnaireAnswers
				If (questionnaireAnswer.QuestionnaireCategory = "QUESTCAT_COI_FA" And questionnaireAnswer.QuestionnaireVersion.Questionnaire.Name = "Trustee's Family Associations and Equity Interests") Then
					currentCOI.RemoveQuestionnaireAnswer(questionnaireAnswer.Id)
				End If
			Next
			End If							
		End If
		
		
		currentCOI.Save(connectionString, PHSRAG.Insight21.COI.Constants.COIDateType.None)
    End Sub
			
End Module